package com.cardbaobao.cardbabyclient.ui.adapter.base;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;

/**
 * 多item类型通用适配器
 *
 * @param <T>
 * @author zad
 */
public abstract class MultiItemCommonAdapter<T> extends CommonAdapter<T> {

    protected MultiItemTypeSupport<T> mMultiItemTypeSupport;

    /**
     * 多item类型通用适配器
     *
     * @param mContext
     * @param mDatas
     * @param multiItemTypeSupport
     */
    public MultiItemCommonAdapter(Context mContext, List<T> mDatas, MultiItemTypeSupport<T> multiItemTypeSupport) {
        super(mContext, mDatas, -1);
        this.mMultiItemTypeSupport = multiItemTypeSupport;
        if (mMultiItemTypeSupport == null) {
            throw new IllegalArgumentException("the mMultiItemTypeSupport can not be null");
        }
    }

    @Override
    public int getViewTypeCount() {
        if (mMultiItemTypeSupport != null) {
            return mMultiItemTypeSupport.getViewTypeCount();
        }
        return super.getViewTypeCount();
    }

    @Override
    public int getItemViewType(int position) {
        if (mMultiItemTypeSupport != null) {
            return mMultiItemTypeSupport.getItemViewType(position, mData.get(position));
        }
        return super.getItemViewType(position);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (mMultiItemTypeSupport == null) {
            return super.getView(position, convertView, parent);
        }
        int layoutId = mMultiItemTypeSupport.getLayoutId(position, getItem(position));//获取位置对应的item布局Id
        final ViewHolder viewHolder = ViewHolder.get(mContext, convertView, parent, layoutId, position);
        convert(viewHolder, getItem(position), position);//回调
        return viewHolder.getConvertView();
    }
}
